package win.ganbo.easyframwork.controller;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import win.ganbo.easyframwork.service.IUserService;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Autowired
    IUserService userService;

    @RequestMapping(path = "login")
    public String login(String username, String password, HttpServletRequest request) {
        System.out.println("username===>" + username + "====password===>" + password);
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        System.out.println("exceptionClassName=============>" + exceptionClassName);
        if (exceptionClassName != null) {
            if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
                request.setAttribute("errorMsg","账号不存在");
            } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
                request.setAttribute("errorMsg","用户名或者密码错误");
            } else if ("randomCodeError".equals(exceptionClassName)) {
                request.setAttribute("errorMsg","验证码错误");
            } else {
                request.setAttribute("errorMsg","其他错误,请联系系统管理员");
            }
        }
        //shiro认证成功会自动跳转到上一个路径
        return "login";
    }


}
